﻿using IBLL;
using IBLL.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace RK0217.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class NumController : ControllerBase
    {
        public NumController(INumBLL bll)
        {
            Bll = bll;
        }

        public INumBLL Bll { get; }


        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddNum(HouseNum num)
        {
            return await Bll.AddNum(num);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DelNum(int id)
        {
            return await Bll.DelNum(id);
        }
        /// <summary>
        /// 根据编号查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public HouseNum GetHouseNum(int id)
        {
            return Bll.GetHouseNum(id);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IQueryable<HouseNum> Show()
        {
            return Bll.Show();
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public PageDto ShowWhere(int index, int size, string? name, int id)
        {
            return Bll.ShowWhere(index, size, name, id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdNum(HouseNum num)
        {
            return await Bll.UpdNum(num);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Export()
        {
            var data = Bll.Show();
            IWorkbook objects= new XSSFWorkbook();
            var sheet = objects.CreateSheet("房号信息表");
            var row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("房号编号");
            row.CreateCell(1).SetCellValue("房号名称");
            row.CreateCell(2).SetCellValue("房号数量");
            row.CreateCell(3).SetCellValue("上否上架");
            var rowindex = 0;
            foreach (var item in data)
            {
                var rows=sheet.CreateRow(rowindex);
                rows.CreateCell(0).SetCellValue(item.Id);
                rows.CreateCell(1).SetCellValue(item.Name);
                rows.CreateCell(2).SetCellValue(item.Num);
                rows.CreateCell(3).SetCellValue(item.IsDeleted?"上架":"下架");
                rowindex++;
            }
            byte[]? bytes = null;
            using (var stream = new MemoryStream())
            {
                objects.Write(stream);
                bytes = stream.ToArray();
            }
            var contentType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes,contentType, "房号信息表.xlsx");
        }
    }
}
